package kr.hkit.androidcomplete02.network;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import kr.hkit.androidcomplete02.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.NetworkOnMainThreadException;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AsyncDownHtml extends Activity {
	ProgressDialog mProgress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.downhtml);

		Button btn = (Button) findViewById(R.id.down);
		btn.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				mProgress = ProgressDialog.show(AsyncDownHtml.this, "Wait",
						"Downloading..");
				DownThread thread = new DownThread("http://www.google.com");
				thread.start();
			}
		});
	}

	class DownThread extends Thread {
		String mAddr;

		DownThread(String addr) {
			mAddr = addr;
		}

		@Override
		public void run() {
			String result = DownloadHtml(mAddr);
			Message message = mAfterDown.obtainMessage();
			message.obj = result;
			mAfterDown.sendMessage(message);
		}

		String DownloadHtml(String addr) {
			StringBuilder html = new StringBuilder();
			try {
				URL url = new URL(addr);
				HttpURLConnection conn = (HttpURLConnection) url
						.openConnection();
				if (conn != null) {
					conn.setConnectTimeout(10000);
					conn.setUseCaches(false);
					if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
						BufferedReader br = new BufferedReader(
								new InputStreamReader(conn.getInputStream()));
						for (;;) {
							String line = br.readLine();
							if (line == null)
								break;
							html.append(line + '\n');
						}
						br.close();
					}
					conn.disconnect();
				}
			} catch (NetworkOnMainThreadException e) {
				return "Error : 메인 스레드 네트워크 작업 에러 - " + e.getMessage();
			} catch (Exception e) {
				return "Error : " + e.getMessage();
			}
			return html.toString();
		}
	}

	Handler mAfterDown = new Handler() {
		public void handleMessage(Message msg) {
			mProgress.dismiss();
			TextView result = (TextView) findViewById(R.id.resulthtml);
			result.setText((String) msg.obj);
		}
	};

}
